#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "newgame.h"
#include "game.h"
#include "nation.h"
#include "nations.h"
#include "csv_read.h"
#include "heat.h"
#include "heat_rider.h"
#include <iostream>
#include <QVector>
#include <QProcess>
#include "time.h"
#include "csv.h"
#include "dmp.h"
#include "team.h"
#include "load_save.h"
#include "match.h"
#include "database.h"
#include <iostream>
using namespace std;




MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ///////////////////////////////////////////////
    db.load();

    //////////////////////////////////////////////
    srand((unsigned)time(0));
    ui->setupUi(this);
    setWindowTitle("Hot Tires");
    combos_a << ui->comb_a0 << ui->comb_a1 << ui->comb_a2 << ui->comb_a3
                << ui->comb_a4 << ui->comb_a5 << ui->comb_a6 << ui->comb_a7;
    combos_h << ui->comb_h0 << ui->comb_h1 << ui->comb_h2 << ui->comb_h3
                << ui->comb_h4 << ui->comb_h5 << ui->comb_h6 << ui->comb_h7;
    db.date.setDate(6,4,2014);

     /*
        QStringList rowData, rowOfData;
            csv_read reader("nations.csv");
            rowOfData = reader.read();
            gamer.nations=new nation[rowOfData.size()];
            gamer.numb_of_nations=rowOfData.size()-1;

          // ui->name_label->setText();

            for (int x = 0; x < rowOfData.size()-1; x++)
            {
                rowData = rowOfData.at(x).split(",");


                gamer.nations[x].nazwa=rowData[1];
                gamer.nations[x].icon.addFile("flags/"+gamer.nations[x].nazwa+".bmp" );



            }
       */
    //load_country();

    //!!!! cos tu okomentowalem jak dobrze pamietam -Kinoll
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::fillDmp()
{
    QStringList fill1 = db.dmps[0].teams[0]->getRidersTextRepresenatation();
    QStringList fill2 = db.dmps[0].teams[1]->getRidersTextRepresenatation();

    for (int i = 0; i < combos_a.size() - 1; i++)
    {
        combos_a[i]->insertItems(0, fill2);
        combos_h[i]->insertItems(0, fill1);
    }
}

void MainWindow::on_but_new_clicked()
{
   newgame *newg = new newgame;
   newg->exec();
   ui->tab_Manager->setEnabled(true);
   ui->tab_Mmain->setCurrentIndex(0);
   ui->tab_Manager->setCurrentIndex(0);
   QString change = gamer.player.name+" "+gamer.player.surname;
   ui->name_label->setText(change);
}

void MainWindow::on_but_load_clicked()
{
    ui->tab_Manager->setEnabled(true);
    ui->tab_Mmain->setCurrentIndex(0);
    ui->tab_Manager->setCurrentIndex(0);
}

void MainWindow::on_name_label_textChanged()
{

}

void MainWindow::on_but_heat_clicked()

{
}

void MainWindow::on_but_match_clicked()
{
}

void MainWindow::on_but_team_apply_clicked()
{
    for (int i = 0; i < combos_h.size(); i++)
        if (combos_h[i]->currentText() != "")
            db.dmps[0].containers[0].add(db.dmps[0].teams[0]->persons[combos_h[i]->currentIndex()]);
    for (int i = 0; i < combos_a.size(); i++)
        if (combos_a[i]->currentText() != "")
            db.dmps[0].containers[1].add(db.dmps[0].teams[1]->persons[combos_a[i]->currentIndex()]);
    db.dmps[0].mergeContainers(db.dmps[0].containers);

}

void MainWindow::on_pushButton_clicked()
{
    if (e->event_type == "dmp" && db.dmps[0].heat_number < 15)
    {
        txt = db.dmps[0].runHeat(db.riders);
        ui->textBrowser_heatDisplay->insertHtml(txt.join(" ")+"<br>");
    }
    else if (e->event_type == "ind16" && db.ind16s[0].heat_number < 20)
    {
        txt = db.ind16s[0].runHeat(db.riders);
        ui->textBrowser_heatDisplay->insertHtml(txt.join(" ")+"<br>");
    }
    else
    {
        if (e->event_type == "dmp")
            db.dmps.removeFirst();
        else if (e->event_type == "ind16")
            db.ind16s.removeFirst();
        ui->textBrowser_heatDisplay->setHtml("");
    }
}

void MainWindow::on_but_continue_clicked()
{
    if (db.events[0]->date == db.date)
    {
        e = db.events.takeFirst();
        if (e->event_type == "dmp")
        {
            fillDmp();
            ui->tab_Mmain->setCurrentIndex(2);
            ui->tab_match_2->setCurrentIndex(1);
        }
        else if (e->event_type == "ind16")
        {
            ui->tab_Mmain->setCurrentIndex(2);
            ui->tab_match_2->setCurrentIndex(0);
        }
    }
    else
        db.date.addDays(1);
}
